<?php

/**
 * 框架DEMO - 数据库
 *
 * @author suning <snsnsky@gmail.com>
 *
 * $Id$
 */

use \Common\Bridge;

require_once('../../code/common.inc.php');

class mysql extends lib\core\Action
{
    public function doDefault()
    {
        $pdo = Bridge::pdo(Bridge::DB_ADMIN_W);

        // PDO 标准用法(查询)
        $query = $pdo->prepare("select * from `Test` WHERE TestId=:id");
        $query->bindValue(':id', '1');
        $query->execute();
        $data = $query->fetchAll();
        // 打印预处理语句
        echo $query->queryString;
        var_dump($data);

        // 其他标准用法 见PHP手册

        // PDO 简化用法(查询)
        // ============================================================
        $pdo->prepare("select * from `Test` WHERE TestId=:id");
        $pdo->bindValue(':id', '2');
        $pdo->execute();
        $data = $pdo->fetchAll();
        var_dump($data);

        // PDO 封装 - 查询
        // ============================================================
        $data1 = $pdo->from('Test')
            ->select("Name,Email")
            ->where(['TestId' => '1'])
            ->limit(0, 1)
            ->getOne();

        $data2 = $pdo->from('Test')
            ->where(['TestId' => '1'])
            ->limit(0, 1)
            ->getAll();

        $data3 = $pdo->find('Test', ['TestId' => 1, 'Name' => 'suning'], 'Name,Email');

        // 支持 '=','<','>','<>','>=','<=','IN','LIKE' 操作符, 更复杂的需要写原始语句
        $data4 = $pdo->find('Test', ['TestId >=' => 1, "TestId IN" => [1, 2]]);

        var_dump($data1);
        var_dump($data2);
        var_dump($data3);
        var_dump($data4);

        // PDO 封装 - 插入
        // ============================================================
        /*
        $count = $pdo->insert('Test', [
            'Name' => 'insert_test',
            'Email' => 'insert@126.com',
            'CreateTime' => date("Y-m-d H:i:s")
        ]);
        // 上次插入ID
        $id = $pdo->lastInsertId();
        var_dump(['count' => $count, 'id' => $id]);
        */

        // PDO 封装 - 更新
        // ============================================================
        $rowcount = $pdo->update('Test', [
            'Name' => 'insert_aaa',
        ], ['TestId' => 3]);
        var_dump($rowcount);

        /* +1/-1
        $rowcount = $pdo->update('Test', [
            'ViewCount' => ['ViewCount', '+1'],
            'ViewCount' => ['ViewCount', '-2']
        ], ['TestId' => 3]);
        var_dump($rowcount);
        */

        // PDO 封装 - 删除
        // ============================================================
        /*
        $rowcount = $pdo->delete('Test', ['TestId' => 3]);
        var_dump($rowcount);
        */

        // PDO 封装 - 插入数据, 若主键存在则更新 ['Name'] 字段的内容, 可指定多个字段.
        // ============================================================
        $pdo->upsert('Test', [
            'TestId' => '10',
            'Name' => 'upsert_test2',
            'Email' => 'upsert_test@126.com',
            'CreateTime' => date("Y-m-d H:i:s")
        ], ['Name']);



    }
}

$app->run();